﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace LBCMS.Helper
{
    public class JsonHelper
    {

        /// <summary>
        /// 将对象序列化为JSON格式
        /// </summary>
        /// <param name="o">对象</param>
        /// <returns>json字符串</returns>
        public static string toJson(object o)
        {
            var aIsoDateTimeConverter = new IsoDateTimeConverter();
            aIsoDateTimeConverter.DateTimeFormat = "yyyy-MM-dd";

            string json = JsonConvert.SerializeObject(o, aIsoDateTimeConverter);
            return json;
        }
        public static string toJsonTime(object o)
        {
            var aIsoDateTimeConverter = new IsoDateTimeConverter();
            aIsoDateTimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            string json = JsonConvert.SerializeObject(o, aIsoDateTimeConverter);
            return json;
        }

        /// <summary>
        /// 指定时间字段格式化标准
        /// </summary>
        /// <param name="o">对象</param>
        /// <param name="timeFormat">yyyy-MM-dd HH:mm:ss</param>
        /// <returns>json字符串</returns>
        public static string toJson(object o,string timeFormat)
        {
            var aIsoDateTimeConverter = new IsoDateTimeConverter();
            aIsoDateTimeConverter.DateTimeFormat = timeFormat;

            string json = JsonConvert.SerializeObject(o, aIsoDateTimeConverter);
            return json;
        }

        /// <summary>
        /// 简化方法，直接返回成功json
        /// liuj 2018-1-3 14:03:49
        /// </summary>
        /// <returns></returns>
        public static string toSuccessJson()
        {
            retInfo ret = new retInfo();
            ret.success = true;
            ret.msg = "操作成功！";

            return toJson(ret);
        }


        /// <summary>
        /// 简化方法，直接返回失败json
        /// liuj 2018-1-3 14:03:42
        /// </summary>
        /// <returns></returns>
        public static string toFailJson()
        {
            retInfo ret = new retInfo();
            ret.success = false;
            ret.msg = "操作失败！";

            return toJson(ret);
        }

        /// <summary>
        /// 解析JSON字符串生成对象实体
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
        /// <returns>对象实体</returns>
        public static T toObj<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T t = o as T;
            return t;
        }
        /// <summary>
        /// 反序列化JSON到给定的匿名对象.
        /// </summary>
        /// <typeparam name="T">匿名对象类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <param name="anonymousTypeObject">匿名对象</param>
        /// <returns>匿名对象</returns>
        public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
        {
            T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
            return t;
        }


     
        public class retInfo
        {
            public bool result { get; set; }
            public bool success { get; set; }
            public string msg { get; set; }

            public string data { get; set; }
        }

        /// <summary>
        /// 通用JSON返回值
        /// </summary>
        /// <typeparam name="T">数据列表的实体类名</typeparam>
        public class retData<T> where T : class
        {
            /// <summary>
            /// 返回结果状态
            /// </summary>
            public bool success { get; set; }
            /// <summary>
            /// 客户端提示语
            /// </summary>
            public string msg { get; set; }
 

            /// <summary>
            /// 数据
            /// </summary>
            public List<T> data { get; set; }

            
        }

        /// <summary>
        /// 通用JSON返回值
        /// </summary>
        /// <typeparam name="T">数据列表的实体类名</typeparam>
        public class retDataSingle<T> where T : class
        {
            public bool result { get; set; }

            /// <summary>
            /// 返回结果状态
            /// </summary>
            public bool success = true;
            /// <summary>
            /// 客户端提示语
            /// </summary>
            public string msg { get; set; }

            /// <summary>
            /// 数据
            /// </summary>
            public T data { get; set; }


        }
    }
}
